//
//  BVCMS
//
//  Created by Allen Thomas on 1/3/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "PersonDetailsConnection.h"
#import "BVCMSAppDelegate.h"
#import "GenericXMLParser.h"

@implementation PersonDetailsConnection

@synthesize delegate;

@synthesize activeDownload;

@synthesize connection;

@synthesize personId;

@synthesize person;

#pragma mark

- (void)dealloc {
	[self cancelDownload];
	
    [activeDownload release];
    [connection cancel];
    [connection release];
	[personId release];
	[person release];
    [super dealloc];	
}

- (void)startDownload {
	BVCMSAppDelegate *appdelegate = [[UIApplication sharedApplication] delegate];
	
    self.activeDownload = [NSMutableData data];
	
	NSString *urlString = [NSString stringWithFormat:@"https://%@/iphone/DetailResults/%@", 
						   appdelegate.cmsServer,
						   personId];
	NSLog(@"%@",urlString);
	
	NSMutableURLRequest *myRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString] ];
	[myRequest setTimeoutInterval:20.0];
	[myRequest addValue:appdelegate.username forHTTPHeaderField:@"username"];
	[myRequest addValue:appdelegate.password forHTTPHeaderField:@"password"];
	[myRequest setHTTPMethod:@"POST"];
	
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:myRequest delegate:self];
	[myRequest release];
	
    self.connection = conn;
	
    [conn release];
	
}

- (void)cancelDownload {
    [self.connection cancel];	
    self.connection = nil;	
    self.activeDownload = nil;	
}

#pragma mark -
#pragma mark Download support (NSURLConnectionDelegate)

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.activeDownload appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    // Clear the activeDownload property to allow later attempts	
    self.activeDownload = nil;
	
    // Release the connection now that it's finished
    self.connection = nil;
	
	[delegate personDetailsFailed:error];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
	NSString *output = [[NSString alloc] initWithData:activeDownload encoding:NSASCIIStringEncoding];
	NSLog(@"%@", output);
	[output release];
	
    // Release the connection now that it's finished
    self.connection = nil;
	
	[self parseXml:activeDownload];
	
    // call our delegate and tell it that our icon is ready for display
    [delegate personDetailsCallback:person];
	
	self.activeDownload = nil;

}


/*
 Creates an xml parser and begins parsing the xml data.
 */
-(void) parseXml:(NSData *)data {
	NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
	
	//Initialize the delegate.
	GenericXMLParser *parser = [[GenericXMLParser alloc] init];
	parser.delegate = self;
	
	//Set delegate
	[xmlParser setDelegate:parser];
	
	//Start parsing the XML file.
	BOOL success = [xmlParser parse];
	
	if(success)
		NSLog(@"No Errors");
	else
		NSLog(@"Error Error Error!!!");
	
	[parser release];
	[xmlParser release];
}

/*
 Callback function that handles the 'person' tag.
 */
-(void) handleElement_Person: (NSDictionary*) attributes {
	NSLog(@"Person handler");
	person = [[PersonDetails alloc] init];
	//person.familyMembers = [[NSMutableArray alloc] init];
	//person.relatives = [[NSMutableArray alloc] init];
	person.details = attributes;
}

-(void) handleElement_OtherMember: (NSDictionary*) attributes {
	NSLog(@"OtherMember handler");
	[person.familyMembers addObject:attributes];
}

-(void) handleElement_RelatedFamily: (NSDictionary*) attributes {
	NSLog(@"RelatedMember handler");
	[person.relatives addObject:attributes];
}




@end
